package com.snail.service.Impl;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import com.snail.model.entity.Product;
import com.snail.model.pojo.Result;
import com.snail.model.pojo.SearchHitResult;
import com.snail.service.BizProductService;
import com.snail.service.HotdataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author cici
 * @description productService的实现类
 * @date 2025/8/19 14:24
 */
@Service
public class BizProductServiceImpl implements BizProductService {
    @Autowired
    HotdataService hotdataService;
    @Autowired
    private ElasticsearchClient elasticsearchClient;
    @Override
    public SearchResponse<Product> searchProductsByClient(String keyword, int page, int size) {
        try {
            return elasticsearchClient.search(s -> s
                            .index("biz_product")
                            .query(q -> q
                                    .bool(b -> b
                                            // must 里面放 multiMatch：匹配 keyword
                                            .must(m -> m
                                                    .multiMatch(mm -> mm
                                                            .fields("product_name", "description")
                                                            .query(keyword)
                                                    )
                                            )
                                            // filter 里面放 deleted=1，不参与打分
                                            .filter(f -> f
                                                    .term(t -> t
                                                            .field("deleted")
                                                            .value("0")
                                                    )
                                            )
                                    )
                            ).from((page - 1) * size) // 起始位置 (offset)
                            .size(size),              // 每页条数,
                    Product.class // 指定返回的文档类型
            );
        } catch (IOException e) {
            throw new RuntimeException("搜索失败", e);
        }
    }

    @Override
    public SearchHitResult<Product> searchAndSerialize(String keyword, int page, int size) {
        SearchHitResult<Product> res=SearchHitResult.<Product>fromSearchHits( this.searchProductsByClient(keyword,page,size));
        return res;
    }
    //clazz 传 (Class<SearchHitResult<Product>>) (Class<?>) SearchHitResult.class 是为了解决泛型擦除问题
    @Override
    public SearchHitResult<Product> SearchAsHot(String keyword, int page, int size) {
        return hotdataService.queryHotData(keyword+"&page:"+page+"&size:"+size,(Class<SearchHitResult<Product>>) (Class<?>) SearchHitResult.class,
                k -> this.searchAndSerialize(k, page, size)) ; // 把 page/size 固定住;
    }

}
